﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.ExceptionManagement;

namespace Kiiro.DataAccess
{
    public class CustomDA
    {
        #region Singleton
        private CustomDA ( )
        {
        }

        private static CustomDA instance = null;
        private static readonly object threadLock = new object ( );

        public static CustomDA Instance
        {
            get
            {
                lock ( threadLock )
                {
                    if ( instance == null )
                    {
                        instance = new CustomDA ( );
                    }
                }
                return instance;
            }
        }
        #endregion

        public SPContext GetCustomFieldsContext (
            Type type,
            int itemId,
            HttpContext httpContext,
            Guid siteCollectionId,
            string serverRelativeUrl )
        {
            SPContext context = null;
            SPWeb web = null;
            SPList list = null;

            try
            {
                web = SPHelpers.GetWeb ( siteCollectionId, serverRelativeUrl );
                web.AllowUnsafeUpdates = true;

                if ( type == typeof ( User ) )
                {
                    if ( SPHelpers.IsPortal ( web ) )
                    {
                        list = web.Lists [ "Users" ];
                    }
                    else if ( SPHelpers.IsProject ( web ) )
                    {
                        list = web.ParentWeb.Lists [ "Users" ];
                        web = web.ParentWeb;
                    }
                }
                else if (type == typeof(Project))
                {
                    if (SPHelpers.IsPortal(web))
                    {
                        list = web.Lists["Projects"];
                    }
                    else if (SPHelpers.IsProject(web))
                    {
                        list = web.ParentWeb.Lists["Projects"];
                        web = web.ParentWeb;
                    }
                }
                else if ( type == typeof ( Work ) )
                {
                    list = web.Lists [ "Work" ];
                }
                else if ( type == typeof ( Discussion ) )
                {
                    list = web.Lists [ "Discussions" ];
                }
                else if ( type == typeof ( TeamMember ) )
                {
                    list = web.Lists [ "Team" ];
                }

                // fix itemId (we can't get a proper SPContext for -1, so change it to 0)
                itemId = itemId == -1 ? 0 : itemId;

                context = SPContext.GetContext ( httpContext, itemId, list.ID, web );
            }
            catch ( Exception ex )
            {
                ExceptionManager.HandleException ( ex, true );
            }
            finally
            {
                if ( web != null )
                {
                    web.AllowUnsafeUpdates = false;
                    web.Dispose ( );
                }
            }

            return context;
        }

        public List<string> GetCustomContentTypeNames ( Guid siteCollectionId, string serverRelativeUrl )
        {
            SPWeb web = null;
            List<string> names = new List<string> ( );
            SPContentType baseCustomContentType = null;

            try
            {
                using ( SPSite site = SPHelpers.GetSystemSite( siteCollectionId ) )
                {
                    using ( web = site.RootWeb )
                    {
                        baseCustomContentType = web.ContentTypes [ new SPContentTypeId ( Constants.SPContentTypeIds.Custom ) ];

                        foreach ( SPContentType contentType in web.ContentTypes )
                        {
                            if ( contentType.Id.IsChildOf ( baseCustomContentType.Id ) && !contentType.Id.Equals ( baseCustomContentType.Id ) )
                            {
                                names.Add ( contentType.Name );
                            }
                        }
                    }
                }
            }
            finally
            {
                if ( web != null )
                {
                    web.Dispose ( );
                }
            }

            return names;
        }

        public List<CustomItem> GetAllCustomItems ( List<CustomItem> customItems, Guid siteCollectionId, string serverRelativeUrl )
        {
            SPWeb web = null;
            List<CustomItem> items = new List<CustomItem> ( );

            try
            {
                web = SPHelpers.GetWeb(siteCollectionId, serverRelativeUrl);
                string serverRelativeWebUrl = SPHelpers.GetPortalServerRelativeUrl(siteCollectionId, serverRelativeUrl);

                foreach (CustomItem customItem in customItems)
                {
                    try
                    {
                        string url = string.Format("{0}{1}/{2}_.000", serverRelativeWebUrl, customItem.ServerRelativeUrl, customItem.Id);
                        Logging.LoggingManager.LogMessage("Trace", "Attempting to get list item at " + url);
                        SPListItem listItem = web.GetListItem(url);
                        items.Add(SPDataHelpers.MakeCustomItem(listItem));
                    }
                    catch (Exception ex)
                    {
                        ExceptionManager.HandleException(ex, false);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ex, true);
            }
            finally
            {
                if ( web != null )
                {
                    web.Dispose ( );
                }
            }

            return items;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.DataAccess/CustomDA.cs $
 * 
 * 7     6/11/09 8:00p Camerons
 * 
 * 6     17/08/09 10:41a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/